angular.module('starter')
// 账户登录
.controller("userLoginCtrl", function ($scope, $state,$location, $ionicLoading, $ionicPopup, authService,appService, accountService, $ionicHistory, $ionicTabsDelegate) {
    $scope.$on("$ionicView.beforeEnter", function () {
        if (authService.authentication.isAuth) {
            console.log("已经登录了, 正在为你跳转到...")
            //$state.go('home');
            //location.href = "/#/userLogin";
            $location.path("selectHospital");
            return;
        }
    });
    var conChange = $(".userLogin .inputMess .username input");
    var undo = $(".userLogin .inputMess .username .undo");
    appService.inputchangeFn($scope, conChange, undo);//调用输入框输入内容显示叉叉图标按钮服务

    //控制密码显示隐藏
    $scope.psd = "password";
    $scope.psdBol = false;
    $scope.psdHandle = true;
    $scope.eyeFn = function () {
        if ($scope.psdHandle) {
            $scope.psd = "text";
            $scope.psdBol = true;
            $scope.psdHandle = false;
        } else {
            $scope.psd = "password";
            $scope.psdBol = false;
            $scope.psdHandle = true;
        }
    }
    $scope.loginData = {
        userName: '',
        password: ''
    };

    //$ionicHistory.clearHistory();

    $ionicTabsDelegate.showBar(false);
 
    $scope.login = function () {

        $scope.loginData.userName = "admin";
        $scope.loginData.password = "1234";
        var userName = $scope.loginData.userName;
        var password = $scope.loginData.password;
 
        if (userName.length == 0) {
            $ionicPopup.alert({
                title: '登录失败',
                template: '用户名不能为空'
            });
            return;
        }
        if (password.length == 0) {
            $ionicPopup.alert({
                title: '登录失败',
                template: '密码不能为空'
            });
            return;
        }

        $ionicLoading.show({
            template: '登录中...'
        });
        authService.login($scope.loginData).then(function (response) {
            // 登录成功
            console.log("登录成功, 正在为你跳转到...");
            accountService.User().then(function (response) {
                $ionicLoading.hide();
                console.log(response);
                //$state.go('home');
                $ionicHistory.goBack();
            }, function (err) {
                console.log(err);
                $ionicLoading.hide();
            });

        }, function (err) {
            console.log(err);
            $ionicLoading.hide();
            $ionicPopup.alert({
                title: '登录失败',
                template: '请检查您填写的登陆信息！'
            });
            $scope.loginData.userName = "";
            $scope.loginData.password = "";
        });
    }

    $scope.backbtn = function ()
    {
        $state.go('home');
        //$location.path("home");
        return;
    }

    initialize();

    function initialize()
    {
        authService.login($scope.loginData).then(function (response) {
            console.log("初始化");
        }, function (err) {
            console.log("初始化");
        });
    }
});